let hasLocalData = false;
const houseId = fxj.getSearch().get('houseId');
const ja = new Vue({
    el: '#main',
    data: (() => {
        let data = JSON.parse(localStorage.getItem('hm_rull'));
        if (!data) {
            data = {
                intervalId: null,
                canCancelOrder: false,
                location: 0, //地址页显示
                add_address_page: 0,
                step2: 0,
                mainpage: 0, //主菜单页显示
                pictureGuid: 0, //图片引导页
                setPicture: 0, //图片设置页
                stepnow: 0, //当前页面
                title: '基本信息', //顶部标题
                bobutton: '完成', //底部按钮
                none_msg: '请点击选择',
                newhouse: 0, //是否为新建房源
                basic: 1, //基础信息页面
                infrastructure: 0, //基础设施
                introduce: 0, //介绍页
                setIntroduce: 0, //介绍页显示
                rotate: 0, //图片旋转
                is_img_rotate: 'addimg_heng', //图片旋转
                add_img_button_div: 1, //
                mask_img_rule: 0, //img规则弹出层
                img_rotate: {
                    transform: '',
                },
                succese_page: 0,
                intro_text: { //介绍页面显示控制
                    area1: 0,
                    area2: 0,
                    area3: 0,
                    area4: 0,
                },

                address_list: {},

                boo_page: {
                    page1: 0,
                    page2: 0,
                    page3: 0,
                    page4: 0,
                },
                send_imgdata: {},
                token: localStorage.token,
                url: 'appv2.fxj178.com/Home/HouseUpload/addAddress',
                add_address: {
                    adcode: '',
                    street: '',
                    houseNumber: '',
                    detailAddress: '',
                    lng: '',
                    lat: '',
                    token: localStorage.token,
                },

                chargeRule: { //发布规则页面控制
                    main: 1,
                    menu: 1,
                    type1: 0,
                    type2: 0,
                    type3: 0,
                    type4: 0,
                    type5: 0,
                    footer: 0,
                },

                publish_rule: { //房源发布规则数据
                    token: localStorage.token,
                    houseId,
                    saveMsg: {
                        discount_date: '',
                        attachment_fare: '',
                        invoice: '0',
                        foreignGuest: '0',
                        min_day: '',
                        max_day: '',
                        cancelPaddingDate: '',
                        noUseDate: '',
                        price: '',
                        require: '',
                        isDiscountSuccessive7: '0',
                        succesive7_discount: '',
                        isDiscountSuccessive30: '0',
                        succesive30_discount: '',
                        isDiscountWeekend: '0',
                        isDiscountWeekend2: '0',
                        discount_price: '',
                        discount_price2: '',
                        pledgeAmount: '',
                    },


                },
                bedSet: { //床位设置页面控制
                    page1: 0,
                    page2: 0,

                },
                text_content: '',
                textarea_number_max: 0,
            };
        }
        else {
            hasLocalData = true;
        }
        return data;
    })(),
    methods: {
        chargeRuleType(type) {
            switch (type) {
                case 1:
                    this.chargeRule.type1 = 1;
                    break;
                case 2:
                    this.chargeRule.type2 = 1;
                    break;
                case 3:
                    this.chargeRule.type3 = 1;
                    break;
                case 4:
                    this.chargeRule.type4 = 1;
                    break;
                case 5:
                    this.chargeRule.type1 = 0;
                    this.chargeRule.type2 = 0;
                    this.chargeRule.type3 = 0;
                    this.chargeRule.type4 = 0;
                    this.chargeRule.menu = 1;
                    this.chargeRule.footer = 0;
                    return;
            }
            this.chargeRule.menu = 0;
            this.chargeRule.footer = 1;
        },

        //发布规则
        async send_publish_rull() {
            const close = fxj.dialog.loading({noBg: true});
            const saveMsg = ja.publish_rule.saveMsg;
            const re = await ajax({
                url: `${fxj.apiServerAddress}/home/${fxj.getSearch()
                    .get('type') === 'add' ? 'houseUpload/ruleAdd' : 'houseManage/houseUpdateRule'}`,
                data: {
                    houseId: houseId,
                    saveMsg: {
                        'discount-date': saveMsg.discount_date,
                        'attachment-fare': saveMsg.attachment_fare,
                        'invoice': saveMsg.invoice,
                        'foreignGuest': saveMsg.foreignGuest,
                        'min-day': saveMsg.min_day,
                        'max-day': saveMsg.max_day,
                        'cancelPaddingDate': saveMsg.cancelPaddingDate,
                        'noUseDate': saveMsg.noUseDate,
                        'price': saveMsg.price,
                        'require': saveMsg.require,
                        'isDiscountSuccessive7': saveMsg.isDiscountSuccessive7,
                        'succesive7-discount': saveMsg.succesive7_discount,
                        'isDiscountSuccessive30': saveMsg.isDiscountSuccessive30,
                        'succesive30-discount': saveMsg.succesive30_discount,
                        'isDiscountWeekend': saveMsg.isDiscountWeekend,
                        'discount-price': saveMsg.discount_price,
                        'pledgeAmount': saveMsg.pledgeAmount,
                        'pledgeMethod': 1,
                        'cancel': ja.canCancelOrder ? 2 : 1, // 1 可以取消订单 2不可以取消订单
                    },
                },
            });
            close();
            if (re.err === 0) {
                if (fxj.getSearch().get('type') === 'add') {
                    clearInterval(ja.intervalId);
                    localStorage.removeItem('hm_rull');
                }
                toast('提交成功', () => {
                    goPage('hm_menu.html', {
                        data: {
                            houseId,
                        },
                    });
                });
            }
            else {
                toast(re.msg);
            }
        },

        weekendSapecial() {
            var that = this;
            if ($('#isDiscountWe').hasClass('mui-active')) {
                that.publish_rule.saveMsg.isDiscountWeekend = 1;
            }
            else {
                that.publish_rule.saveMsg.isDiscountWeekend = 0;
                that.publish_rule.saveMsg.discount_date = 0;
            }
        },
        ren_long_days() {
            var that = this;
            if ($('#rent_long_days').hasClass('mui-active')) {
                that.publish_rule.saveMsg.isDiscountSuccessive7 = 1;
            }
            else {
                that.publish_rule.saveMsg.isDiscountSuccessive7 = 0;

            }

        },
        ren_long_days1() {
            var that = this;
            if ($('#rent_long_days1').hasClass('mui-active')) {
                that.publish_rule.saveMsg.isDiscountSuccessive30 = 1;
            }
            else {
                that.publish_rule.saveMsg.isDiscountSuccessive30 = 0;
            }

        },
        is_forein_goust() {
            var that = this;
            if ($('#forein_goust').hasClass('mui-active')) {
                that.publish_rule.saveMsg.foreignGuest = 1;

            }
            else {
                that.publish_rule.saveMsg.foreignGuest = 0;
            }
        },
    },
});

async function created() {
    const close = fxj.dialog.loading({noBg: true});
    const re = await ajax({
        url: 'home/houseDetail/getHouseDetail',
        data: {
            houseId,
        },
    });
    close();
    if (re.err === 0) {
        ja.canCancelOrder = re.data.notcancel === '2';
        ja.publish_rule.saveMsg.discount_date = re.data.discount_date;
        ja.publish_rule.saveMsg.invoice = re.data.invoice;
        ja.publish_rule.saveMsg.foreignGuest = re.data.abroad;

        ja.publish_rule.saveMsg.min_day = re.data.day_least;
        $('#least_day').text(ja.publish_rule.saveMsg.min_day + '天');
        ja.publish_rule.saveMsg.max_day = re.data.day_most;
        $('#most_day').text(ja.publish_rule.saveMsg.max_day !== '0' ? ja.publish_rule.saveMsg.max_day + '天' : '不限');
        ja.publish_rule.saveMsg.price = re.data.price;
        ja.publish_rule.saveMsg.require = re.data.demand;
        ja.publish_rule.saveMsg.cancelPaddingDate = re.data.cancel_day;
        ja.publish_rule.saveMsg.pledgeAmount = re.data.deposit;
        ja.publish_rule.saveMsg.noUseDate = re.data.cancel_deduct;

        if (re.data.seven_rebate) {
            ja.publish_rule.saveMsg.isDiscountSuccessive7 = 1;
            ja.publish_rule.saveMsg.succesive7_discount = re.data.seven_rebate;
            $('#rent_long_days').addClass('mui-active');
            $('#day7').text(re.data.seven_rebate);
        }
        else {
            ja.publish_rule.saveMsg.isDiscountSuccessive7 = 0;
            ja.publish_rule.saveMsg.succesive7_discount = 1;
        }

        if (re.data.thirty_rebate) {
            ja.publish_rule.saveMsg.isDiscountSuccessive30 = 1;
            ja.publish_rule.saveMsg.succesive30_discount = re.data.thirty_rebate;
            $('#rent_long_days1').addClass('mui-active');
            $('#day30').text(re.data.thirty_rebate);
        }
        else {
            ja.publish_rule.saveMsg.isDiscountSuccessive30 = 0;
            ja.publish_rule.saveMsg.succesive30_discount = 1;
        }

        if (re.data.sunday_price !== '0' || re.data.saturday_price !== '0') {
            ja.publish_rule.saveMsg.isDiscountWeekend = 1;
            $('#isDiscountWe').addClass('mui-active');
        }
        else {
            ja.publish_rule.saveMsg.isDiscountWeekend = 0;
        }

        const price_6 = re.data.saturday_price;
        const price_7 = re.data.sunday_price;
        ja.publish_rule.saveMsg.discount_price = price_6 !== '0' ? price_6 : price_7 !== '0' ? price_7 : '';
        ja.publish_rule.saveMsg.attachment_fare = re.data.premium;
        document.getElementById('wekends').innerHTML = `${price_6 !== '0' ? '周六' : ''}${(price_6 !== '0' && price_7 !== '0') ? '/' : ''}${price_7 !== '0' ? '周日' : ''}`;
        ja.publish_rule.saveMsg.discount_date = (() => {
            if (price_6 !== '0' && price_7 !== '0') return '3';
            if (price_6 !== '0') return '1';
            if (price_7 !== '0') return '2';
        })();
    }
    initPicker();
}

function apiready() {
    created();
    if (hasLocalData) {
        let txt = (() => {
            switch (ja.publish_rule.saveMsg.discount_date) {
                case '1':
                    return '周六';
                case '2':
                    return '周日';
                case '3':
                    return '周六/周日';
            }
        })();
        if (txt) {
            document.getElementById('wekends').innerHTML = txt;

            if (ja.publish_rule.saveMsg.succesive7_discount) {
                document.getElementById('day7').innerHTML = ja.publish_rule.saveMsg.succesive7_discount;
            }
            if (ja.publish_rule.saveMsg.succesive30_discount) {
                document.getElementById('day30').innerHTML = ja.publish_rule.saveMsg.succesive30_discount;
            }
        }
        if (ja.publish_rule.saveMsg.min_day) {
            document.getElementById('least_day').innerHTML = ja.publish_rule.saveMsg.min_day + '天';
        }
        if (ja.publish_rule.saveMsg.max_day) {
            document.getElementById('most_day').innerHTML = (() => {
                switch (ja.publish_rule.saveMsg.max_day) {
                    case '0':
                        return '不限';
                    case '180':
                        return '180天+';
                    default:
                        return ja.publish_rule.saveMsg.max_day + '天';
                }
            })();
        }
    }

}

const cutselect = new MobileSelect({
    trigger: '#day7',
    title: '折扣选择',
    wheels: [
        {
            data: [
                {
                    id: '9',
                    value: '9',
                },
                {
                    id: '8',
                    value: '8',
                },
                {
                    id: '7',
                    value: '7',
                },
                {
                    id: '6',
                    value: '6',
                },
                {
                    id: '5',
                    value: '5',
                },
                {
                    id: '4',
                    value: '4',
                },
                {
                    id: '3',
                    value: '3',
                },
                {
                    id: '2',
                    value: '2',
                },
                {
                    id: '1',
                    value: '1',
                },
            ],
        },
        {
            data: [{
                id: '9',
                value: '.9',
            },
                {
                    id: '8',
                    value: '.8',
                },
                {
                    id: '7',
                    value: '.7',
                },
                {
                    id: '6',
                    value: '.6',
                },
                {
                    id: '5',
                    value: '.5',
                },
                {
                    id: '4',
                    value: '.4',
                },
                {
                    id: '3',
                    value: '.3',
                },
                {
                    id: '2',
                    value: '.2',
                },
                {
                    id: '1',
                    value: '.1',
                },
                {
                    id: '0',
                    value: '.0',
                },
            ],
        },
    ],
    callback: function (indexArr, data) {
        ja.publish_rule.saveMsg.succesive7_discount = (data[0].id + data[1].id) / 10;
        if (!ja.publish_rule.saveMsg.succesive7_discount) {
            $('#rent_long_days').removeClass('mui-active');
        }
    },
});

const housearea = new MobileSelect({
    trigger: '#day30',
    title: '折扣选择',
    wheels: [{
        data: [{
            id: '9',
            value: '9',
        },
            {
                id: '8',
                value: '8',
            },
            {
                id: '7',
                value: '7',
            },
            {
                id: '6',
                value: '6',
            },
            {
                id: '5',
                value: '5',
            },
            {
                id: '4',
                value: '4',
            },
            {
                id: '3',
                value: '3',
            },
            {
                id: '2',
                value: '2',
            },
            {
                id: '1',
                value: '1',
            },
        ],
    },
        {
            data: [{
                id: '9',
                value: '.9',
            },
                {
                    id: '8',
                    value: '.8',
                },
                {
                    id: '7',
                    value: '.7',
                },
                {
                    id: '6',
                    value: '.6',
                },
                {
                    id: '5',
                    value: '.5',
                },
                {
                    id: '4',
                    value: '.4',
                },
                {
                    id: '3',
                    value: '.3',
                },
                {
                    id: '2',
                    value: '.2',
                },
                {
                    id: '1',
                    value: '.1',
                },
                {
                    id: '0',
                    value: '.0',
                },
            ],
        },
    ],
    callback: function (indexArr, data) {

        ja.publish_rule.saveMsg.succesive30_discount = (data[0].id + data[1].id) / 10;
        if (!ja.publish_rule.saveMsg.succesive30_discount) {
            $('#rent_long_days1').removeClass('mui-active');
        }
    },
});

const people_max = new MobileSelect({
    trigger: '#least_day',
    title: '最少入住天数',
    wheels: [{
        data: [{
            id: '1',
            value: '1天',
        },
            {
                id: '2',
                value: '2天',
            },
            {
                id: '3',
                value: '3天',
            },
            {
                id: '4',
                value: '4天',
            },
            {
                id: '5',
                value: '5天',
            },
            {
                id: '6',
                value: '6天',
            },
            {
                id: '7',
                value: '7天',
            },
            {
                id: '8',
                value: '8天',
            },
            {
                id: '9',
                value: '9天',
            },

        ],
    }],
    callback: function (indexArr, data) {
        ja.publish_rule.saveMsg.min_day = data[0].id;
    },
});

const people_max2 = new MobileSelect({
    trigger: '#most_day',
    title: '最多入住天数',
    wheels: [{
        data: [{
            id: '0',
            value: '不限',
        },
            {
                id: '5',
                value: '5天',
            },
            {
                id: '10',
                value: '10天',
            },
            {
                id: '15',
                value: '15天',
            },
            {
                id: '20',
                value: '20天',
            },
            {
                id: '30',
                value: '30天',
            },
            {
                id: '40',
                value: '40天',
            },
            {
                id: '50',
                value: '50天',
            },
            {
                id: '60',
                value: '60天',
            },
            {
                id: '90',
                value: '90天',
            },
            {
                id: '120',
                value: '120天',
            },
            {
                id: '180',
                value: '180天+',
            },

        ],
    }],
    callback: function (indexArr, data) {
        ja.publish_rule.saveMsg.max_day = data[0].id;
    },
});

function initPicker() {
    const nameEl = document.getElementById('wekends');
    const data = [
        {
            text: '周六',
            value: '1',
        },
        {
            text: '周日',
            value: '2',
        },
        {
            text: '周六/周日',
            value: '3',
        },
    ];
    const picker = new Picker({
        data: [data],
        selectedIndex: [0],
        title: '请选择',
    });
    picker.on('picker.valuechange', function (selectedVal, selectedIndex) {
        ja.publish_rule.saveMsg.discount_date = selectedVal[0];
        nameEl.innerHTML = data[selectedIndex].text;
    });
    document.getElementById('wekendsContainer').addEventListener('click', function () {
        picker.show();
    });
}

function checkIpt() {
    ja.publish_rule.saveMsg.pledgeAmount = ja.publish_rule.saveMsg.pledgeAmount.toString().split('').filter(char => {
        return /\d/.test(char);
    }).join('');
}

if (fxj.getSearch().get('type') === 'add') {
    ja.intervalId = setInterval(() => {
        localStorage.setItem('hm_rull', JSON.stringify(ja.$data));
    }, 2000);
}
